home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / ZSI / wstools / XMLSchema.pyo (.txt) < prev   
Python Compiled Bytecode  |  2008-10-13  |  79KB  |  2,742 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. ident = '$Id: XMLSchema.py 1434 2007-11-01 22:42:47Z boverhof $'
  5. import types
  6. import weakref
  7. import sys
  8. import warnings
  9. from Namespaces import SCHEMA, XMLNS, SOAP
  10. from Utility import DOM, DOMException, Collection, SplitQName, basejoin
  11. from StringIO import StringIO
  12.  
  13. try:
  14.     from threading import RLock
  15. except ImportError:
  16.     
  17.     class RLock:
  18.         
  19.         def acquire():
  20.             pass
  21.  
  22.         
  23.         def release():
  24.             pass
  25.  
  26.  
  27.  
  28. TYPES = 'types'
  29. ATTRIBUTE_GROUPS = 'attr_groups'
  30. ATTRIBUTES = 'attr_decl'
  31. ELEMENTS = 'elements'
  32. MODEL_GROUPS = 'model_groups'
  33. BUILT_IN_NAMESPACES = [
  34.     SOAP.ENC] + SCHEMA.XSD_LIST
  35.  
  36. def GetSchema(component):
  37.     parent = component
  38.     while not isinstance(parent, XMLSchema):
  39.         parent = parent._parent()
  40.     return parent
  41.  
  42.  
  43. class SchemaReader:
  44.     namespaceToSchema = { }
  45.     
  46.     def __init__(self, domReader = None, base_url = None):
  47.         self._SchemaReader__base_url = base_url
  48.         self._SchemaReader__readerClass = domReader
  49.         if not self._SchemaReader__readerClass:
  50.             self._SchemaReader__readerClass = DOMAdapter
  51.         
  52.         self._includes = { }
  53.         self._imports = { }
  54.  
  55.     
  56.     def __setImports(self, schema):
  57.         for ns, val in schema.imports.items():
  58.             if self._imports.has_key(ns):
  59.                 schema.addImportSchema(self._imports[ns])
  60.                 continue
  61.         
  62.  
  63.     
  64.     def __setIncludes(self, schema):
  65.         for schemaLocation, val in schema.includes.items():
  66.             if self._includes.has_key(schemaLocation):
  67.                 schema.addIncludeSchema(schemaLocation, self._imports[schemaLocation])
  68.                 continue
  69.         
  70.  
  71.     
  72.     def addSchemaByLocation(self, location, schema):
  73.         self._includes[location] = schema
  74.  
  75.     
  76.     def addSchemaByNamespace(self, schema):
  77.         self._imports[schema.targetNamespace] = schema
  78.  
  79.     
  80.     def loadFromNode(self, parent, element):
  81.         reader = self._SchemaReader__readerClass(element)
  82.         schema = XMLSchema(parent)
  83.         schema.wsdl = parent
  84.         schema.setBaseUrl(self._SchemaReader__base_url)
  85.         schema.load(reader)
  86.         return schema
  87.  
  88.     
  89.     def loadFromStream(self, file, url = None):
  90.         reader = self._SchemaReader__readerClass()
  91.         reader.loadDocument(file)
  92.         schema = XMLSchema()
  93.         if url is not None:
  94.             schema.setBaseUrl(url)
  95.         
  96.         schema.load(reader)
  97.         self._SchemaReader__setIncludes(schema)
  98.         self._SchemaReader__setImports(schema)
  99.         return schema
  100.  
  101.     
  102.     def loadFromString(self, data):
  103.         return self.loadFromStream(StringIO(data))
  104.  
  105.     
  106.     def loadFromURL(self, url, schema = None):
  107.         reader = self._SchemaReader__readerClass()
  108.         if self._SchemaReader__base_url:
  109.             url = basejoin(self._SchemaReader__base_url, url)
  110.         
  111.         reader.loadFromURL(url)
  112.         if not schema:
  113.             pass
  114.         schema = XMLSchema()
  115.         schema.setBaseUrl(url)
  116.         schema.load(reader)
  117.         self._SchemaReader__setIncludes(schema)
  118.         self._SchemaReader__setImports(schema)
  119.         return schema
  120.  
  121.     
  122.     def loadFromFile(self, filename):
  123.         if self._SchemaReader__base_url:
  124.             filename = basejoin(self._SchemaReader__base_url, filename)
  125.         
  126.         file = open(filename, 'rb')
  127.         
  128.         try:
  129.             schema = self.loadFromStream(file, filename)
  130.         finally:
  131.             file.close()
  132.  
  133.         return schema
  134.  
  135.  
  136.  
  137. class SchemaError(Exception):
  138.     pass
  139.  
  140.  
  141. class NoSchemaLocationWarning(Exception):
  142.     pass
  143.  
  144.  
  145. class DOMAdapterInterface:
  146.     
  147.     def hasattr(self, attr, ns = None):
  148.         raise NotImplementedError, 'adapter method not implemented'
  149.  
  150.     
  151.     def getContentList(self, *contents):
  152.         raise NotImplementedError, 'adapter method not implemented'
  153.  
  154.     
  155.     def setAttributeDictionary(self, attributes):
  156.         raise NotImplementedError, 'adapter method not implemented'
  157.  
  158.     
  159.     def getAttributeDictionary(self):
  160.         raise NotImplementedError, 'adapter method not implemented'
  161.  
  162.     
  163.     def getNamespace(self, prefix):
  164.         raise NotImplementedError, 'adapter method not implemented'
  165.  
  166.     
  167.     def getTagName(self):
  168.         raise NotImplementedError, 'adapter method not implemented'
  169.  
  170.     
  171.     def getParentNode(self):
  172.         raise NotImplementedError, 'adapter method not implemented'
  173.  
  174.     
  175.     def loadDocument(self, file):
  176.         raise NotImplementedError, 'adapter method not implemented'
  177.  
  178.     
  179.     def loadFromURL(self, url):
  180.         raise NotImplementedError, 'adapter method not implemented'
  181.  
  182.  
  183.  
  184. class DOMAdapter(DOMAdapterInterface):
  185.     
  186.     def __init__(self, node = None):
  187.         if hasattr(node, 'documentElement'):
  188.             self._DOMAdapter__node = node.documentElement
  189.         else:
  190.             self._DOMAdapter__node = node
  191.         self._DOMAdapter__attributes = None
  192.  
  193.     
  194.     def getNode(self):
  195.         return self._DOMAdapter__node
  196.  
  197.     
  198.     def hasattr(self, attr, ns = None):
  199.         if not self._DOMAdapter__attributes:
  200.             self.setAttributeDictionary()
  201.         
  202.         if ns:
  203.             return self._DOMAdapter__attributes.get(ns, { }).has_key(attr)
  204.         
  205.         return self._DOMAdapter__attributes.has_key(attr)
  206.  
  207.     
  208.     def getContentList(self, *contents):
  209.         nodes = []
  210.         ELEMENT_NODE = self._DOMAdapter__node.ELEMENT_NODE
  211.         for child in DOM.getElements(self._DOMAdapter__node, None):
  212.             if child.nodeType == ELEMENT_NODE and SplitQName(child.tagName)[1] in contents:
  213.                 nodes.append(child)
  214.                 continue
  215.         
  216.         return map(self.__class__, nodes)
  217.  
  218.     
  219.     def setAttributeDictionary(self):
  220.         self._DOMAdapter__attributes = { }
  221.         for v in self._DOMAdapter__node._attrs.values():
  222.             self._DOMAdapter__attributes[v.nodeName] = v.nodeValue
  223.         
  224.  
  225.     
  226.     def getAttributeDictionary(self):
  227.         if not self._DOMAdapter__attributes:
  228.             self.setAttributeDictionary()
  229.         
  230.         return self._DOMAdapter__attributes
  231.  
  232.     
  233.     def getTagName(self):
  234.         return self._DOMAdapter__node.tagName
  235.  
  236.     
  237.     def getParentNode(self):
  238.         if self._DOMAdapter__node.parentNode.nodeType == self._DOMAdapter__node.ELEMENT_NODE:
  239.             return DOMAdapter(self._DOMAdapter__node.parentNode)
  240.         
  241.  
  242.     
  243.     def getNamespace(self, prefix):
  244.         namespace = None
  245.         if prefix == 'xmlns':
  246.             namespace = DOM.findDefaultNS(prefix, self._DOMAdapter__node)
  247.         else:
  248.             
  249.             try:
  250.                 namespace = DOM.findNamespaceURI(prefix, self._DOMAdapter__node)
  251.             except DOMException:
  252.                 ex = None
  253.                 if prefix != 'xml':
  254.                     raise SchemaError, '%s namespace not declared for %s' % (prefix, self._DOMAdapter__node._get_tagName())
  255.                 
  256.                 namespace = XMLNS.XML
  257.  
  258.         return namespace
  259.  
  260.     
  261.     def loadDocument(self, file):
  262.         self._DOMAdapter__node = DOM.loadDocument(file)
  263.         if hasattr(self._DOMAdapter__node, 'documentElement'):
  264.             self._DOMAdapter__node = self._DOMAdapter__node.documentElement
  265.         
  266.  
  267.     
  268.     def loadFromURL(self, url):
  269.         self._DOMAdapter__node = DOM.loadFromURL(url)
  270.         if hasattr(self._DOMAdapter__node, 'documentElement'):
  271.             self._DOMAdapter__node = self._DOMAdapter__node.documentElement
  272.         
  273.  
  274.  
  275.  
  276. class XMLBase:
  277.     tag = None
  278.     __indent = 0
  279.     __rlock = RLock()
  280.     
  281.     def __str__(self):
  282.         XMLBase._XMLBase__rlock.acquire()
  283.         XMLBase._XMLBase__indent += 1
  284.         tmp = '<' + str(self.__class__) + '>\n'
  285.         for k, v in self.__dict__.items():
  286.             tmp += '%s* %s = %s\n' % (XMLBase._XMLBase__indent * '  ', k, v)
  287.         
  288.         XMLBase._XMLBase__indent -= 1
  289.         XMLBase._XMLBase__rlock.release()
  290.         return tmp
  291.  
  292.  
  293.  
  294. class DefinitionMarker:
  295.     pass
  296.  
  297.  
  298. class DeclarationMarker:
  299.     pass
  300.  
  301.  
  302. class AttributeMarker:
  303.     pass
  304.  
  305.  
  306. class AttributeGroupMarker:
  307.     pass
  308.  
  309.  
  310. class WildCardMarker:
  311.     pass
  312.  
  313.  
  314. class ElementMarker:
  315.     pass
  316.  
  317.  
  318. class ReferenceMarker:
  319.     pass
  320.  
  321.  
  322. class ModelGroupMarker:
  323.     pass
  324.  
  325.  
  326. class AllMarker(ModelGroupMarker):
  327.     pass
  328.  
  329.  
  330. class ChoiceMarker(ModelGroupMarker):
  331.     pass
  332.  
  333.  
  334. class SequenceMarker(ModelGroupMarker):
  335.     pass
  336.  
  337.  
  338. class ExtensionMarker:
  339.     pass
  340.  
  341.  
  342. class RestrictionMarker:
  343.     facets = [
  344.         'enumeration',
  345.         'length',
  346.         'maxExclusive',
  347.         'maxInclusive',
  348.         'maxLength',
  349.         'minExclusive',
  350.         'minInclusive',
  351.         'minLength',
  352.         'pattern',
  353.         'fractionDigits',
  354.         'totalDigits',
  355.         'whiteSpace']
  356.  
  357.  
  358. class SimpleMarker:
  359.     pass
  360.  
  361.  
  362. class ListMarker:
  363.     pass
  364.  
  365.  
  366. class UnionMarker:
  367.     pass
  368.  
  369.  
  370. class ComplexMarker:
  371.     pass
  372.  
  373.  
  374. class LocalMarker:
  375.     pass
  376.  
  377.  
  378. class MarkerInterface:
  379.     
  380.     def isDefinition(self):
  381.         return isinstance(self, DefinitionMarker)
  382.  
  383.     
  384.     def isDeclaration(self):
  385.         return isinstance(self, DeclarationMarker)
  386.  
  387.     
  388.     def isAttribute(self):
  389.         return isinstance(self, AttributeMarker)
  390.  
  391.     
  392.     def isAttributeGroup(self):
  393.         return isinstance(self, AttributeGroupMarker)
  394.  
  395.     
  396.     def isElement(self):
  397.         return isinstance(self, ElementMarker)
  398.  
  399.     
  400.     def isReference(self):
  401.         return isinstance(self, ReferenceMarker)
  402.  
  403.     
  404.     def isWildCard(self):
  405.         return isinstance(self, WildCardMarker)
  406.  
  407.     
  408.     def isModelGroup(self):
  409.         return isinstance(self, ModelGroupMarker)
  410.  
  411.     
  412.     def isAll(self):
  413.         return isinstance(self, AllMarker)
  414.  
  415.     
  416.     def isChoice(self):
  417.         return isinstance(self, ChoiceMarker)
  418.  
  419.     
  420.     def isSequence(self):
  421.         return isinstance(self, SequenceMarker)
  422.  
  423.     
  424.     def isExtension(self):
  425.         return isinstance(self, ExtensionMarker)
  426.  
  427.     
  428.     def isRestriction(self):
  429.         return isinstance(self, RestrictionMarker)
  430.  
  431.     
  432.     def isSimple(self):
  433.         return isinstance(self, SimpleMarker)
  434.  
  435.     
  436.     def isComplex(self):
  437.         return isinstance(self, ComplexMarker)
  438.  
  439.     
  440.     def isLocal(self):
  441.         return isinstance(self, LocalMarker)
  442.  
  443.     
  444.     def isList(self):
  445.         return isinstance(self, ListMarker)
  446.  
  447.     
  448.     def isUnion(self):
  449.         return isinstance(self, UnionMarker)
  450.  
  451.  
  452.  
  453. class XMLSchemaComponent(XMLBase, MarkerInterface):
  454.     required = []
  455.     attributes = { }
  456.     contents = { }
  457.     xmlns_key = ''
  458.     xmlns = 'xmlns'
  459.     xml = 'xml'
  460.     
  461.     def __init__(self, parent = None):
  462.         self.attributes = None
  463.         self._parent = parent
  464.         if self._parent:
  465.             self._parent = weakref.ref(parent)
  466.         
  467.         if not (self.__class__ == XMLSchemaComponent):
  468.             if type(self.__class__.required) == type(XMLSchemaComponent.required) and type(self.__class__.attributes) == type(XMLSchemaComponent.attributes):
  469.                 pass
  470.             if not (type(self.__class__.contents) == type(XMLSchemaComponent.contents)):
  471.                 raise RuntimeError, 'Bad type for a class variable in %s' % self.__class__
  472.             
  473.  
  474.     
  475.     def getItemTrace(self):
  476.         (item, path, name, ref) = (self, [], 'name', 'ref')
  477.         while not isinstance(item, XMLSchema) and not isinstance(item, WSDLToolsAdapter):
  478.             attr = item.getAttribute(name)
  479.             if not attr:
  480.                 attr = item.getAttribute(ref)
  481.                 if not attr:
  482.                     path.append('<%s>' % item.tag)
  483.                 else:
  484.                     path.append('<%s ref="%s">' % (item.tag, attr))
  485.             else:
  486.                 path.append('<%s name="%s">' % (item.tag, attr))
  487.             item = item._parent()
  488.         
  489.         try:
  490.             tns = item.getTargetNamespace()
  491.         except:
  492.             tns = ''
  493.  
  494.         path.append('<%s targetNamespace="%s">' % (item.tag, tns))
  495.         path.reverse()
  496.         return ''.join(path)
  497.  
  498.     
  499.     def getTargetNamespace(self):
  500.         parent = self
  501.         targetNamespace = 'targetNamespace'
  502.         tns = self.attributes.get(targetNamespace)
  503.         while not tns and parent and parent._parent is not None:
  504.             parent = parent._parent()
  505.             tns = parent.attributes.get(targetNamespace)
  506.         if not tns:
  507.             pass
  508.         return ''
  509.  
  510.     
  511.     def getAttributeDeclaration(self, attribute):
  512.         return self.getQNameAttribute(ATTRIBUTES, attribute)
  513.  
  514.     
  515.     def getAttributeGroup(self, attribute):
  516.         return self.getQNameAttribute(ATTRIBUTE_GROUPS, attribute)
  517.  
  518.     
  519.     def getTypeDefinition(self, attribute):
  520.         return self.getQNameAttribute(TYPES, attribute)
  521.  
  522.     
  523.     def getElementDeclaration(self, attribute):
  524.         return self.getQNameAttribute(ELEMENTS, attribute)
  525.  
  526.     
  527.     def getModelGroup(self, attribute):
  528.         return self.getQNameAttribute(MODEL_GROUPS, attribute)
  529.  
  530.     
  531.     def getQNameAttribute(self, collection, attribute):
  532.         tdc = self.getAttributeQName(attribute)
  533.         if not tdc:
  534.             return None
  535.         
  536.         obj = self.getSchemaItem(collection, tdc.getTargetNamespace(), tdc.getName())
  537.         if obj:
  538.             return obj
  539.         
  540.  
  541.     
  542.     def getSchemaItem(self, collection, namespace, name):
  543.         parent = GetSchema(self)
  544.         if parent.targetNamespace == namespace:
  545.             
  546.             try:
  547.                 obj = getattr(parent, collection)[name]
  548.             except KeyError:
  549.                 ex = None
  550.                 raise KeyError, 'targetNamespace(%s) collection(%s) has no item(%s)' % (namespace, collection, name)
  551.  
  552.             return obj
  553.         
  554.         if not parent.imports.has_key(namespace):
  555.             if namespace in BUILT_IN_NAMESPACES:
  556.                 return None
  557.             
  558.             raise SchemaError, 'schema "%s" does not import namespace "%s"' % (parent.targetNamespace, namespace)
  559.         
  560.         schema = parent.imports[namespace]
  561.         if not isinstance(schema, XMLSchema):
  562.             schema = schema.getSchema()
  563.             if schema is not None:
  564.                 parent.imports[namespace] = schema
  565.             
  566.         
  567.         if schema is None:
  568.             if namespace in BUILT_IN_NAMESPACES:
  569.                 return None
  570.             
  571.             raise SchemaError, 'no schema instance for imported namespace (%s).' % namespace
  572.         
  573.         if not isinstance(schema, XMLSchema):
  574.             raise TypeError, 'expecting XMLSchema instance not "%r"' % schema
  575.         
  576.         
  577.         try:
  578.             obj = getattr(schema, collection)[name]
  579.         except KeyError:
  580.             ex = None
  581.             raise KeyError, 'targetNamespace(%s) collection(%s) has no item(%s)' % (namespace, collection, name)
  582.  
  583.         return obj
  584.  
  585.     
  586.     def getXMLNS(self, prefix = None):
  587.         if prefix == XMLSchemaComponent.xml:
  588.             return XMLNS.XML
  589.         
  590.         parent = self
  591.         if not prefix:
  592.             pass
  593.         ns = self.attributes[XMLSchemaComponent.xmlns].get(XMLSchemaComponent.xmlns_key)
  594.         while not ns:
  595.             parent = parent._parent()
  596.             if not prefix:
  597.                 pass
  598.             ns = parent.attributes[XMLSchemaComponent.xmlns].get(XMLSchemaComponent.xmlns_key)
  599.             if not ns and isinstance(parent, WSDLToolsAdapter):
  600.                 if prefix is None:
  601.                     return ''
  602.                 
  603.                 raise SchemaError, 'unknown prefix %s' % prefix
  604.                 continue
  605.         return ns
  606.  
  607.     
  608.     def getAttribute(self, attribute):
  609.         if type(attribute) in (list, tuple):
  610.             if len(attribute) != 2:
  611.                 raise LookupError, 'To access attributes must use name or (namespace,name)'
  612.             
  613.             ns_dict = self.attributes.get(attribute[0])
  614.             if ns_dict is None:
  615.                 return None
  616.             
  617.             return ns_dict.get(attribute[1])
  618.         
  619.         return self.attributes.get(attribute)
  620.  
  621.     
  622.     def getAttributeQName(self, attribute):
  623.         qname = self.getAttribute(attribute)
  624.         if isinstance(qname, TypeDescriptionComponent) is True:
  625.             return qname
  626.         
  627.         if qname is None:
  628.             return None
  629.         
  630.         (prefix, ncname) = SplitQName(qname)
  631.         namespace = self.getXMLNS(prefix)
  632.         return TypeDescriptionComponent((namespace, ncname))
  633.  
  634.     
  635.     def getAttributeName(self):
  636.         return self.getAttribute('name')
  637.  
  638.     
  639.     def setAttributes(self, node):
  640.         self.attributes = {
  641.             XMLSchemaComponent.xmlns: { } }
  642.         for k, v in node.getAttributeDictionary().items():
  643.             (prefix, value) = SplitQName(k)
  644.             if value == XMLSchemaComponent.xmlns:
  645.                 if not prefix:
  646.                     pass
  647.                 self.attributes[value][XMLSchemaComponent.xmlns_key] = v
  648.                 continue
  649.             if prefix:
  650.                 ns = node.getNamespace(prefix)
  651.                 if not ns:
  652.                     raise SchemaError, 'no namespace for attribute prefix %s' % prefix
  653.                 
  654.                 if not self.attributes.has_key(ns):
  655.                     self.attributes[ns] = { }
  656.                 elif self.attributes[ns].has_key(value):
  657.                     raise SchemaError, 'attribute %s declared multiple times in %s' % (value, ns)
  658.                 
  659.                 self.attributes[ns][value] = v
  660.                 continue
  661.             if not self.attributes.has_key(value):
  662.                 self.attributes[value] = v
  663.                 continue
  664.             raise SchemaError, 'attribute %s declared multiple times' % value
  665.         
  666.         if not isinstance(self, WSDLToolsAdapter):
  667.             self._XMLSchemaComponent__checkAttributes()
  668.         
  669.         self._XMLSchemaComponent__setAttributeDefaults()
  670.         for k in [
  671.             'type',
  672.             'element',
  673.             'base',
  674.             'ref',
  675.             'substitutionGroup',
  676.             'itemType']:
  677.             if self.attributes.has_key(k):
  678.                 (prefix, value) = SplitQName(self.attributes.get(k))
  679.                 self.attributes[k] = TypeDescriptionComponent((self.getXMLNS(prefix), value))
  680.                 continue
  681.         
  682.         for k in [
  683.             'memberTypes']:
  684.             if self.attributes.has_key(k):
  685.                 qnames = self.attributes[k]
  686.                 self.attributes[k] = []
  687.                 for qname in qnames.split():
  688.                     (prefix, value) = SplitQName(qname)
  689.                     self.attributes['memberTypes'].append(TypeDescriptionComponent((self.getXMLNS(prefix), value)))
  690.                 
  691.         
  692.  
  693.     
  694.     def getContents(self, node):
  695.         return node.getContentList(*self.__class__.contents['xsd'])
  696.  
  697.     
  698.     def __setAttributeDefaults(self):
  699.         for k, v in self.__class__.attributes.items():
  700.             if v is not None and self.attributes.has_key(k) is False:
  701.                 if isinstance(v, types.FunctionType):
  702.                     self.attributes[k] = v(self)
  703.                 else:
  704.                     self.attributes[k] = v
  705.             isinstance(v, types.FunctionType)
  706.         
  707.  
  708.     
  709.     def __checkAttributes(self):
  710.         for a in self.__class__.required:
  711.             if not self.attributes.has_key(a):
  712.                 raise SchemaError, 'class instance %s, missing required attribute %s' % (self.__class__, a)
  713.                 continue
  714.         
  715.         for a, v in self.attributes.items():
  716.             if type(v) is dict:
  717.                 continue
  718.             
  719.             if a in (XMLSchemaComponent.xmlns, XMLNS.XML):
  720.                 continue
  721.             
  722.             if a not in self.__class__.attributes.keys():
  723.                 if self.isAttribute():
  724.                     pass
  725.                 if not self.isReference():
  726.                     raise SchemaError, '%s, unknown attribute(%s,%s)' % (self.getItemTrace(), a, self.attributes[a])
  727.                     continue
  728.         
  729.  
  730.  
  731.  
  732. class WSDLToolsAdapter(XMLSchemaComponent):
  733.     attributes = {
  734.         'name': None,
  735.         'targetNamespace': None }
  736.     tag = 'definitions'
  737.     
  738.     def __init__(self, wsdl):
  739.         XMLSchemaComponent.__init__(self, parent = wsdl)
  740.         self.setAttributes(DOMAdapter(wsdl.document))
  741.  
  742.     
  743.     def getImportSchemas(self):
  744.         return self._parent().types
  745.  
  746.  
  747.  
  748. class Notation(XMLSchemaComponent):
  749.     required = [
  750.         'name',
  751.         'public']
  752.     attributes = {
  753.         'id': None,
  754.         'name': None,
  755.         'public': None,
  756.         'system': None }
  757.     contents = {
  758.         'xsd': 'annotation' }
  759.     tag = 'notation'
  760.     
  761.     def __init__(self, parent):
  762.         XMLSchemaComponent.__init__(self, parent)
  763.         self.annotation = None
  764.  
  765.     
  766.     def fromDom(self, node):
  767.         self.setAttributes(node)
  768.         contents = self.getContents(node)
  769.         for i in contents:
  770.             component = SplitQName(i.getTagName())[1]
  771.             if component == 'annotation' and not (self.annotation):
  772.                 self.annotation = Annotation(self)
  773.                 self.annotation.fromDom(i)
  774.                 continue
  775.             raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  776.         
  777.  
  778.  
  779.  
  780. class Annotation(XMLSchemaComponent):
  781.     attributes = {
  782.         'id': None }
  783.     contents = {
  784.         'xsd': ('documentation', 'appinfo') }
  785.     tag = 'annotation'
  786.     
  787.     def __init__(self, parent):
  788.         XMLSchemaComponent.__init__(self, parent)
  789.         self.content = None
  790.  
  791.     
  792.     def fromDom(self, node):
  793.         self.setAttributes(node)
  794.         contents = self.getContents(node)
  795.         content = []
  796.         for i in contents:
  797.             component = SplitQName(i.getTagName())[1]
  798.             if component == 'documentation':
  799.                 continue
  800.                 continue
  801.             if component == 'appinfo':
  802.                 continue
  803.                 continue
  804.             raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  805.         
  806.         self.content = tuple(content)
  807.  
  808.     
  809.     class Documentation(XMLSchemaComponent):
  810.         attributes = {
  811.             'source': None,
  812.             'xml:lang': None }
  813.         contents = {
  814.             'xsd': ('mixed', 'any') }
  815.         tag = 'documentation'
  816.         
  817.         def __init__(self, parent):
  818.             XMLSchemaComponent.__init__(self, parent)
  819.             self.content = None
  820.  
  821.         
  822.         def fromDom(self, node):
  823.             self.setAttributes(node)
  824.             contents = self.getContents(node)
  825.             content = []
  826.             for i in contents:
  827.                 component = SplitQName(i.getTagName())[1]
  828.                 if component == 'mixed':
  829.                     continue
  830.                     continue
  831.                 if component == 'any':
  832.                     continue
  833.                     continue
  834.                 raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  835.             
  836.             self.content = tuple(content)
  837.  
  838.  
  839.     
  840.     class Appinfo(XMLSchemaComponent):
  841.         attributes = {
  842.             'source': None,
  843.             'anyURI': None }
  844.         contents = {
  845.             'xsd': ('mixed', 'any') }
  846.         tag = 'appinfo'
  847.         
  848.         def __init__(self, parent):
  849.             XMLSchemaComponent.__init__(self, parent)
  850.             self.content = None
  851.  
  852.         
  853.         def fromDom(self, node):
  854.             self.setAttributes(node)
  855.             contents = self.getContents(node)
  856.             content = []
  857.             for i in contents:
  858.                 component = SplitQName(i.getTagName())[1]
  859.                 if component == 'mixed':
  860.                     continue
  861.                     continue
  862.                 if component == 'any':
  863.                     continue
  864.                     continue
  865.                 raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  866.             
  867.             self.content = tuple(content)
  868.  
  869.  
  870.  
  871.  
  872. class XMLSchemaFake:
  873.     
  874.     def __init__(self, element):
  875.         self.targetNamespace = DOM.getAttr(element, 'targetNamespace')
  876.         self.element = element
  877.  
  878.  
  879.  
  880. class XMLSchema(XMLSchemaComponent):
  881.     attributes = {
  882.         'id': None,
  883.         'version': None,
  884.         'xml:lang': None,
  885.         'targetNamespace': None,
  886.         'attributeFormDefault': 'unqualified',
  887.         'elementFormDefault': 'unqualified',
  888.         'blockDefault': None,
  889.         'finalDefault': None }
  890.     contents = {
  891.         'xsd': ('include', 'import', 'redefine', 'annotation', 'attribute', 'attributeGroup', 'complexType', 'element', 'group', 'notation', 'simpleType', 'annotation') }
  892.     empty_namespace = ''
  893.     tag = 'schema'
  894.     
  895.     def __init__(self, parent = None):
  896.         self._XMLSchema__node = None
  897.         self.targetNamespace = None
  898.         XMLSchemaComponent.__init__(self, parent)
  899.         
  900.         f = lambda k: k.attributes['name']
  901.         
  902.         ns = lambda k: k.attributes['namespace']
  903.         
  904.         sl = lambda k: k.attributes['schemaLocation']
  905.         self.includes = Collection(self, key = sl)
  906.         self.imports = Collection(self, key = ns)
  907.         self.elements = Collection(self, key = f)
  908.         self.types = Collection(self, key = f)
  909.         self.attr_decl = Collection(self, key = f)
  910.         self.attr_groups = Collection(self, key = f)
  911.         self.model_groups = Collection(self, key = f)
  912.         self.notations = Collection(self, key = f)
  913.         self._imported_schemas = { }
  914.         self._included_schemas = { }
  915.         self._base_url = None
  916.  
  917.     
  918.     def getNode(self):
  919.         return self._XMLSchema__node
  920.  
  921.     
  922.     def addImportSchema(self, schema):
  923.         if not isinstance(schema, XMLSchema):
  924.             raise TypeError, 'expecting a Schema instance'
  925.         
  926.         if schema.targetNamespace != self.targetNamespace:
  927.             self._imported_schemas[schema.targetNamespace] = schema
  928.         else:
  929.             raise SchemaError, 'import schema bad targetNamespace'
  930.  
  931.     
  932.     def addIncludeSchema(self, schemaLocation, schema):
  933.         if not isinstance(schema, XMLSchema):
  934.             raise TypeError, 'expecting a Schema instance'
  935.         
  936.         if not (schema.targetNamespace) or schema.targetNamespace == self.targetNamespace:
  937.             self._included_schemas[schemaLocation] = schema
  938.         else:
  939.             raise SchemaError, 'include schema bad targetNamespace'
  940.  
  941.     
  942.     def setImportSchemas(self, schema_dict):
  943.         self._imported_schemas = schema_dict
  944.  
  945.     
  946.     def getImportSchemas(self):
  947.         return self._imported_schemas
  948.  
  949.     
  950.     def getSchemaNamespacesToImport(self):
  951.         return tuple(self.includes.keys())
  952.  
  953.     
  954.     def setIncludeSchemas(self, schema_dict):
  955.         self._included_schemas = schema_dict
  956.  
  957.     
  958.     def getIncludeSchemas(self):
  959.         return self._included_schemas
  960.  
  961.     
  962.     def getBaseUrl(self):
  963.         return self._base_url
  964.  
  965.     
  966.     def setBaseUrl(self, url):
  967.         self._base_url = url
  968.  
  969.     
  970.     def getElementFormDefault(self):
  971.         return self.attributes.get('elementFormDefault')
  972.  
  973.     
  974.     def isElementFormDefaultQualified(self):
  975.         return self.attributes.get('elementFormDefault') == 'qualified'
  976.  
  977.     
  978.     def getAttributeFormDefault(self):
  979.         return self.attributes.get('attributeFormDefault')
  980.  
  981.     
  982.     def getBlockDefault(self):
  983.         return self.attributes.get('blockDefault')
  984.  
  985.     
  986.     def getFinalDefault(self):
  987.         return self.attributes.get('finalDefault')
  988.  
  989.     
  990.     def load(self, node, location = None):
  991.         self._XMLSchema__node = node
  992.         pnode = node.getParentNode()
  993.         if pnode:
  994.             pname = SplitQName(pnode.getTagName())[1]
  995.             if pname == 'types':
  996.                 attributes = { }
  997.                 self.setAttributes(pnode)
  998.                 attributes.update(self.attributes)
  999.                 self.setAttributes(node)
  1000.                 for k, v in attributes['xmlns'].items():
  1001.                     if not self.attributes['xmlns'].has_key(k):
  1002.                         self.attributes['xmlns'][k] = v
  1003.                         continue
  1004.                 
  1005.             else:
  1006.                 self.setAttributes(node)
  1007.         else:
  1008.             self.setAttributes(node)
  1009.         self.targetNamespace = self.getTargetNamespace()
  1010.         for childNode in self.getContents(node):
  1011.             component = SplitQName(childNode.getTagName())[1]
  1012.             if component == 'include':
  1013.                 tp = self.__class__.Include(self)
  1014.                 tp.fromDom(childNode)
  1015.                 sl = tp.attributes['schemaLocation']
  1016.                 schema = tp.getSchema()
  1017.                 if not self.getIncludeSchemas().has_key(sl):
  1018.                     self.addIncludeSchema(sl, schema)
  1019.                 
  1020.                 self.includes[sl] = tp
  1021.                 pn = childNode.getParentNode().getNode()
  1022.                 pn.removeChild(childNode.getNode())
  1023.                 for child in schema.getNode().getNode().childNodes:
  1024.                     pn.appendChild(child.cloneNode(1))
  1025.                 
  1026.                 for collection in [
  1027.                     'imports',
  1028.                     'elements',
  1029.                     'types',
  1030.                     'attr_decl',
  1031.                     'attr_groups',
  1032.                     'model_groups',
  1033.                     'notations']:
  1034.                     for k, v in getattr(schema, collection).items():
  1035.                         if not getattr(self, collection).has_key(k):
  1036.                             v._parent = weakref.ref(self)
  1037.                             getattr(self, collection)[k] = v
  1038.                             continue
  1039.                         warnings.warn('Not keeping schema component.')
  1040.                     
  1041.                 
  1042.             if component == 'import':
  1043.                 slocd = SchemaReader.namespaceToSchema
  1044.                 tp = self.__class__.Import(self)
  1045.                 tp.fromDom(childNode)
  1046.                 if not tp.getAttribute('namespace'):
  1047.                     pass
  1048.                 import_ns = self.__class__.empty_namespace
  1049.                 schema = slocd.get(import_ns)
  1050.                 if schema is None:
  1051.                     schema = XMLSchema()
  1052.                     slocd[import_ns] = schema
  1053.                     
  1054.                     try:
  1055.                         tp.loadSchema(schema)
  1056.                     except NoSchemaLocationWarning:
  1057.                         ex = None
  1058.                         del slocd[import_ns]
  1059.                         continue
  1060.                     except SchemaError:
  1061.                         ex = None
  1062.                         warnings.warn('<import namespace="%s">, %s' % (import_ns, 'failed to load schema instance, resort to lazy eval when necessary'))
  1063.                         del slocd[import_ns]
  1064.                         
  1065.                         class _LazyEvalImport('_LazyEvalImport', (str,)):
  1066.                             
  1067.                             def getSchema(namespace):
  1068.                                 schema = slocd.get(namespace)
  1069.                                 if schema is None:
  1070.                                     parent = self._parent()
  1071.                                     wstypes = parent
  1072.                                     if isinstance(parent, WSDLToolsAdapter):
  1073.                                         wstypes = parent.getImportSchemas()
  1074.                                     
  1075.                                     schema = wstypes.get(namespace)
  1076.                                 
  1077.                                 if isinstance(schema, XMLSchema):
  1078.                                     self.imports[namespace] = schema
  1079.                                     return schema
  1080.                                 
  1081.  
  1082.  
  1083.                         self.imports[import_ns] = _LazyEvalImport(import_ns)
  1084.                         continue
  1085.                     except:
  1086.                         None<EXCEPTION MATCH>NoSchemaLocationWarning
  1087.                     
  1088.  
  1089.                 None<EXCEPTION MATCH>NoSchemaLocationWarning
  1090.                 tp._schema = schema
  1091.                 if self.getImportSchemas().has_key(import_ns):
  1092.                     warnings.warn('Detected multiple imports of the namespace "%s" ' % import_ns)
  1093.                 
  1094.                 self.addImportSchema(schema)
  1095.                 self.imports[import_ns] = tp
  1096.                 continue
  1097.             if component == 'redefine':
  1098.                 warnings.warn('redefine is ignored')
  1099.                 continue
  1100.             if component == 'annotation':
  1101.                 warnings.warn('annotation is ignored')
  1102.                 continue
  1103.             if component == 'attribute':
  1104.                 tp = AttributeDeclaration(self)
  1105.                 tp.fromDom(childNode)
  1106.                 self.attr_decl[tp.getAttribute('name')] = tp
  1107.                 continue
  1108.             if component == 'attributeGroup':
  1109.                 tp = AttributeGroupDefinition(self)
  1110.                 tp.fromDom(childNode)
  1111.                 self.attr_groups[tp.getAttribute('name')] = tp
  1112.                 continue
  1113.             if component == 'element':
  1114.                 tp = ElementDeclaration(self)
  1115.                 tp.fromDom(childNode)
  1116.                 self.elements[tp.getAttribute('name')] = tp
  1117.                 continue
  1118.             if component == 'group':
  1119.                 tp = ModelGroupDefinition(self)
  1120.                 tp.fromDom(childNode)
  1121.                 self.model_groups[tp.getAttribute('name')] = tp
  1122.                 continue
  1123.             if component == 'notation':
  1124.                 tp = Notation(self)
  1125.                 tp.fromDom(childNode)
  1126.                 self.notations[tp.getAttribute('name')] = tp
  1127.                 continue
  1128.             if component == 'complexType':
  1129.                 tp = ComplexType(self)
  1130.                 tp.fromDom(childNode)
  1131.                 self.types[tp.getAttribute('name')] = tp
  1132.                 continue
  1133.             if component == 'simpleType':
  1134.                 tp = SimpleType(self)
  1135.                 tp.fromDom(childNode)
  1136.                 self.types[tp.getAttribute('name')] = tp
  1137.                 continue
  1138.             break
  1139.         
  1140.  
  1141.     
  1142.     class Import(XMLSchemaComponent):
  1143.         attributes = {
  1144.             'id': None,
  1145.             'namespace': None,
  1146.             'schemaLocation': None }
  1147.         contents = {
  1148.             'xsd': [
  1149.                 'annotation'] }
  1150.         tag = 'import'
  1151.         
  1152.         def __init__(self, parent):
  1153.             XMLSchemaComponent.__init__(self, parent)
  1154.             self.annotation = None
  1155.             self._schema = None
  1156.  
  1157.         
  1158.         def fromDom(self, node):
  1159.             self.setAttributes(node)
  1160.             contents = self.getContents(node)
  1161.             if self.attributes['namespace'] == self.getTargetNamespace():
  1162.                 raise SchemaError, 'namespace of schema and import match'
  1163.             
  1164.             for i in contents:
  1165.                 component = SplitQName(i.getTagName())[1]
  1166.                 if component == 'annotation' and not (self.annotation):
  1167.                     self.annotation = Annotation(self)
  1168.                     self.annotation.fromDom(i)
  1169.                     continue
  1170.                 raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1171.             
  1172.  
  1173.         
  1174.         def getSchema(self):
  1175.             if not self._schema:
  1176.                 ns = self.attributes['namespace']
  1177.                 schema = self._parent().getImportSchemas().get(ns)
  1178.                 if not schema and self._parent()._parent:
  1179.                     schema = self._parent()._parent().getImportSchemas().get(ns)
  1180.                 
  1181.                 if not schema:
  1182.                     url = self.attributes.get('schemaLocation')
  1183.                     if not url:
  1184.                         raise SchemaError, 'namespace(%s) is unknown' % ns
  1185.                     
  1186.                     base_url = self._parent().getBaseUrl()
  1187.                     reader = SchemaReader(base_url = base_url)
  1188.                     reader._imports = self._parent().getImportSchemas()
  1189.                     reader._includes = self._parent().getIncludeSchemas()
  1190.                     self._schema = reader.loadFromURL(url)
  1191.                 
  1192.             
  1193.             if not self._schema:
  1194.                 pass
  1195.             return schema
  1196.  
  1197.         
  1198.         def loadSchema(self, schema):
  1199.             base_url = self._parent().getBaseUrl()
  1200.             reader = SchemaReader(base_url = base_url)
  1201.             reader._imports = self._parent().getImportSchemas()
  1202.             reader._includes = self._parent().getIncludeSchemas()
  1203.             self._schema = schema
  1204.             if not self.attributes.has_key('schemaLocation'):
  1205.                 raise NoSchemaLocationWarning('no schemaLocation attribute in import')
  1206.             
  1207.             reader.loadFromURL(self.attributes.get('schemaLocation'), schema)
  1208.  
  1209.  
  1210.     
  1211.     class Include(XMLSchemaComponent):
  1212.         required = [
  1213.             'schemaLocation']
  1214.         attributes = {
  1215.             'id': None,
  1216.             'schemaLocation': None }
  1217.         contents = {
  1218.             'xsd': [
  1219.                 'annotation'] }
  1220.         tag = 'include'
  1221.         
  1222.         def __init__(self, parent):
  1223.             XMLSchemaComponent.__init__(self, parent)
  1224.             self.annotation = None
  1225.             self._schema = None
  1226.  
  1227.         
  1228.         def fromDom(self, node):
  1229.             self.setAttributes(node)
  1230.             contents = self.getContents(node)
  1231.             for i in contents:
  1232.                 component = SplitQName(i.getTagName())[1]
  1233.                 if component == 'annotation' and not (self.annotation):
  1234.                     self.annotation = Annotation(self)
  1235.                     self.annotation.fromDom(i)
  1236.                     continue
  1237.                 raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1238.             
  1239.  
  1240.         
  1241.         def getSchema(self):
  1242.             if not self._schema:
  1243.                 schema = self._parent()
  1244.                 self._schema = schema.getIncludeSchemas().get(self.attributes['schemaLocation'])
  1245.                 if not self._schema:
  1246.                     url = self.attributes['schemaLocation']
  1247.                     reader = SchemaReader(base_url = schema.getBaseUrl())
  1248.                     reader._imports = schema.getImportSchemas()
  1249.                     reader._includes = schema.getIncludeSchemas()
  1250.                     self._schema = XMLSchema(schema)
  1251.                     reader.loadFromURL(url, self._schema)
  1252.                 
  1253.             
  1254.             return self._schema
  1255.  
  1256.  
  1257.  
  1258.  
  1259. class AttributeDeclaration(XMLSchemaComponent, AttributeMarker, DeclarationMarker):
  1260.     required = [
  1261.         'name']
  1262.     attributes = {
  1263.         'id': None,
  1264.         'name': None,
  1265.         'type': None,
  1266.         'default': None,
  1267.         'fixed': None }
  1268.     contents = {
  1269.         'xsd': [
  1270.             'annotation',
  1271.             'simpleType'] }
  1272.     tag = 'attribute'
  1273.     
  1274.     def __init__(self, parent):
  1275.         XMLSchemaComponent.__init__(self, parent)
  1276.         self.annotation = None
  1277.         self.content = None
  1278.  
  1279.     
  1280.     def fromDom(self, node):
  1281.         self.setAttributes(node)
  1282.         contents = self.getContents(node)
  1283.         for i in contents:
  1284.             component = SplitQName(i.getTagName())[1]
  1285.             if component == 'annotation' and not (self.annotation):
  1286.                 self.annotation = Annotation(self)
  1287.                 self.annotation.fromDom(i)
  1288.                 continue
  1289.             if component == 'simpleType':
  1290.                 self.content = AnonymousSimpleType(self)
  1291.                 self.content.fromDom(i)
  1292.                 continue
  1293.             raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1294.         
  1295.  
  1296.  
  1297.  
  1298. class LocalAttributeDeclaration(AttributeDeclaration, AttributeMarker, LocalMarker, DeclarationMarker):
  1299.     required = [
  1300.         'name']
  1301.     attributes = {
  1302.         'id': None,
  1303.         'name': None,
  1304.         'type': None,
  1305.         'form': (lambda self: GetSchema(self).getAttributeFormDefault()),
  1306.         'use': 'optional',
  1307.         'default': None,
  1308.         'fixed': None }
  1309.     contents = {
  1310.         'xsd': [
  1311.             'annotation',
  1312.             'simpleType'] }
  1313.     
  1314.     def __init__(self, parent):
  1315.         AttributeDeclaration.__init__(self, parent)
  1316.         self.annotation = None
  1317.         self.content = None
  1318.  
  1319.     
  1320.     def fromDom(self, node):
  1321.         self.setAttributes(node)
  1322.         contents = self.getContents(node)
  1323.         for i in contents:
  1324.             component = SplitQName(i.getTagName())[1]
  1325.             if component == 'annotation' and not (self.annotation):
  1326.                 self.annotation = Annotation(self)
  1327.                 self.annotation.fromDom(i)
  1328.                 continue
  1329.             if component == 'simpleType':
  1330.                 self.content = AnonymousSimpleType(self)
  1331.                 self.content.fromDom(i)
  1332.                 continue
  1333.             raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1334.         
  1335.  
  1336.  
  1337.  
  1338. class AttributeWildCard(XMLSchemaComponent, AttributeMarker, DeclarationMarker, WildCardMarker):
  1339.     attributes = {
  1340.         'id': None,
  1341.         'namespace': '##any',
  1342.         'processContents': 'strict' }
  1343.     contents = {
  1344.         'xsd': [
  1345.             'annotation'] }
  1346.     tag = 'anyAttribute'
  1347.     
  1348.     def __init__(self, parent):
  1349.         XMLSchemaComponent.__init__(self, parent)
  1350.         self.annotation = None
  1351.  
  1352.     
  1353.     def fromDom(self, node):
  1354.         self.setAttributes(node)
  1355.         contents = self.getContents(node)
  1356.         for i in contents:
  1357.             component = SplitQName(i.getTagName())[1]
  1358.             if component == 'annotation' and not (self.annotation):
  1359.                 self.annotation = Annotation(self)
  1360.                 self.annotation.fromDom(i)
  1361.                 continue
  1362.             raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1363.         
  1364.  
  1365.  
  1366.  
  1367. class AttributeReference(XMLSchemaComponent, AttributeMarker, ReferenceMarker):
  1368.     required = [
  1369.         'ref']
  1370.     attributes = {
  1371.         'id': None,
  1372.         'ref': None,
  1373.         'use': 'optional',
  1374.         'default': None,
  1375.         'fixed': None }
  1376.     contents = {
  1377.         'xsd': [
  1378.             'annotation'] }
  1379.     tag = 'attribute'
  1380.     
  1381.     def __init__(self, parent):
  1382.         XMLSchemaComponent.__init__(self, parent)
  1383.         self.annotation = None
  1384.  
  1385.     
  1386.     def getAttributeDeclaration(self, attribute = 'ref'):
  1387.         return XMLSchemaComponent.getAttributeDeclaration(self, attribute)
  1388.  
  1389.     
  1390.     def fromDom(self, node):
  1391.         self.setAttributes(node)
  1392.         contents = self.getContents(node)
  1393.         for i in contents:
  1394.             component = SplitQName(i.getTagName())[1]
  1395.             if component == 'annotation' and not (self.annotation):
  1396.                 self.annotation = Annotation(self)
  1397.                 self.annotation.fromDom(i)
  1398.                 continue
  1399.             raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1400.         
  1401.  
  1402.  
  1403.  
  1404. class AttributeGroupDefinition(XMLSchemaComponent, AttributeGroupMarker, DefinitionMarker):
  1405.     required = [
  1406.         'name']
  1407.     attributes = {
  1408.         'id': None,
  1409.         'name': None }
  1410.     contents = {
  1411.         'xsd': [
  1412.             'annotation',
  1413.             'attribute',
  1414.             'attributeGroup',
  1415.             'anyAttribute'] }
  1416.     tag = 'attributeGroup'
  1417.     
  1418.     def __init__(self, parent):
  1419.         XMLSchemaComponent.__init__(self, parent)
  1420.         self.annotation = None
  1421.         self.attr_content = None
  1422.  
  1423.     
  1424.     def getAttributeContent(self):
  1425.         return self.attr_content
  1426.  
  1427.     
  1428.     def fromDom(self, node):
  1429.         self.setAttributes(node)
  1430.         contents = self.getContents(node)
  1431.         content = []
  1432.         for indx in range(len(contents)):
  1433.             component = SplitQName(contents[indx].getTagName())[1]
  1434.             if component == 'annotation' and not indx:
  1435.                 self.annotation = Annotation(self)
  1436.                 self.annotation.fromDom(contents[indx])
  1437.                 continue
  1438.             if component == 'attribute':
  1439.                 if contents[indx].hasattr('name'):
  1440.                     content.append(LocalAttributeDeclaration(self))
  1441.                 elif contents[indx].hasattr('ref'):
  1442.                     content.append(AttributeReference(self))
  1443.                 else:
  1444.                     raise SchemaError, 'Unknown attribute type'
  1445.                 content[-1].fromDom(contents[indx])
  1446.                 continue
  1447.             if component == 'attributeGroup':
  1448.                 content.append(AttributeGroupReference(self))
  1449.                 content[-1].fromDom(contents[indx])
  1450.                 continue
  1451.             if component == 'anyAttribute':
  1452.                 if len(contents) != indx + 1:
  1453.                     raise SchemaError, 'anyAttribute is out of order in %s' % self.getItemTrace()
  1454.                 
  1455.                 content.append(AttributeWildCard(self))
  1456.                 content[-1].fromDom(contents[indx])
  1457.                 continue
  1458.             raise SchemaError, 'Unknown component (%s)' % contents[indx].getTagName()
  1459.         
  1460.         self.attr_content = tuple(content)
  1461.  
  1462.  
  1463.  
  1464. class AttributeGroupReference(XMLSchemaComponent, AttributeGroupMarker, ReferenceMarker):
  1465.     required = [
  1466.         'ref']
  1467.     attributes = {
  1468.         'id': None,
  1469.         'ref': None }
  1470.     contents = {
  1471.         'xsd': [
  1472.             'annotation'] }
  1473.     tag = 'attributeGroup'
  1474.     
  1475.     def __init__(self, parent):
  1476.         XMLSchemaComponent.__init__(self, parent)
  1477.         self.annotation = None
  1478.  
  1479.     
  1480.     def getAttributeGroup(self, attribute = 'ref'):
  1481.         return XMLSchemaComponent.getAttributeGroup(self, attribute)
  1482.  
  1483.     
  1484.     def fromDom(self, node):
  1485.         self.setAttributes(node)
  1486.         contents = self.getContents(node)
  1487.         for i in contents:
  1488.             component = SplitQName(i.getTagName())[1]
  1489.             if component == 'annotation' and not (self.annotation):
  1490.                 self.annotation = Annotation(self)
  1491.                 self.annotation.fromDom(i)
  1492.                 continue
  1493.             raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1494.         
  1495.  
  1496.  
  1497.  
  1498. class IdentityConstrants(XMLSchemaComponent):
  1499.     
  1500.     def __init__(self, parent):
  1501.         XMLSchemaComponent.__init__(self, parent)
  1502.         self.selector = None
  1503.         self.fields = None
  1504.         self.annotation = None
  1505.  
  1506.     
  1507.     def fromDom(self, node):
  1508.         self.setAttributes(node)
  1509.         contents = self.getContents(node)
  1510.         fields = []
  1511.         for i in contents:
  1512.             component = SplitQName(i.getTagName())[1]
  1513.             if component in self.__class__.contents['xsd']:
  1514.                 if component == 'annotation' and not (self.annotation):
  1515.                     self.annotation = Annotation(self)
  1516.                     self.annotation.fromDom(i)
  1517.                 elif component == 'selector':
  1518.                     self.selector = self.Selector(self)
  1519.                     self.selector.fromDom(i)
  1520.                     continue
  1521.                 elif component == 'field':
  1522.                     fields.append(self.Field(self))
  1523.                     fields[-1].fromDom(i)
  1524.                     continue
  1525.                 else:
  1526.                     raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1527.             else:
  1528.                 raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1529.             self.fields = tuple(fields)
  1530.         
  1531.  
  1532.     
  1533.     class Constraint(XMLSchemaComponent):
  1534.         
  1535.         def __init__(self, parent):
  1536.             XMLSchemaComponent.__init__(self, parent)
  1537.             self.annotation = None
  1538.  
  1539.         
  1540.         def fromDom(self, node):
  1541.             self.setAttributes(node)
  1542.             contents = self.getContents(node)
  1543.             for i in contents:
  1544.                 component = SplitQName(i.getTagName())[1]
  1545.                 if component in self.__class__.contents['xsd']:
  1546.                     if component == 'annotation' and not (self.annotation):
  1547.                         self.annotation = Annotation(self)
  1548.                         self.annotation.fromDom(i)
  1549.                     else:
  1550.                         raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1551.                 not (self.annotation)
  1552.                 raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1553.             
  1554.  
  1555.  
  1556.     
  1557.     class Selector(Constraint):
  1558.         required = [
  1559.             'xpath']
  1560.         attributes = {
  1561.             'id': None,
  1562.             'xpath': None }
  1563.         contents = {
  1564.             'xsd': [
  1565.                 'annotation'] }
  1566.         tag = 'selector'
  1567.  
  1568.     
  1569.     class Field(Constraint):
  1570.         required = [
  1571.             'xpath']
  1572.         attributes = {
  1573.             'id': None,
  1574.             'xpath': None }
  1575.         contents = {
  1576.             'xsd': [
  1577.                 'annotation'] }
  1578.         tag = 'field'
  1579.  
  1580.  
  1581.  
  1582. class Unique(IdentityConstrants):
  1583.     required = [
  1584.         'name']
  1585.     attributes = {
  1586.         'id': None,
  1587.         'name': None }
  1588.     contents = {
  1589.         'xsd': [
  1590.             'annotation',
  1591.             'selector',
  1592.             'field'] }
  1593.     tag = 'unique'
  1594.  
  1595.  
  1596. class Key(IdentityConstrants):
  1597.     required = [
  1598.         'name']
  1599.     attributes = {
  1600.         'id': None,
  1601.         'name': None }
  1602.     contents = {
  1603.         'xsd': [
  1604.             'annotation',
  1605.             'selector',
  1606.             'field'] }
  1607.     tag = 'key'
  1608.  
  1609.  
  1610. class KeyRef(IdentityConstrants):
  1611.     required = [
  1612.         'name',
  1613.         'refer']
  1614.     attributes = {
  1615.         'id': None,
  1616.         'name': None,
  1617.         'refer': None }
  1618.     contents = {
  1619.         'xsd': [
  1620.             'annotation',
  1621.             'selector',
  1622.             'field'] }
  1623.     tag = 'keyref'
  1624.  
  1625.  
  1626. class ElementDeclaration(XMLSchemaComponent, ElementMarker, DeclarationMarker):
  1627.     required = [
  1628.         'name']
  1629.     attributes = {
  1630.         'id': None,
  1631.         'name': None,
  1632.         'type': None,
  1633.         'default': None,
  1634.         'fixed': None,
  1635.         'nillable': 0,
  1636.         'abstract': 0,
  1637.         'substitutionGroup': None,
  1638.         'block': (lambda self: self._parent().getBlockDefault()),
  1639.         'final': (lambda self: self._parent().getFinalDefault()) }
  1640.     contents = {
  1641.         'xsd': [
  1642.             'annotation',
  1643.             'simpleType',
  1644.             'complexType',
  1645.             'key',
  1646.             'keyref',
  1647.             'unique'] }
  1648.     tag = 'element'
  1649.     
  1650.     def __init__(self, parent):
  1651.         XMLSchemaComponent.__init__(self, parent)
  1652.         self.annotation = None
  1653.         self.content = None
  1654.         self.constraints = ()
  1655.  
  1656.     
  1657.     def isQualified(self):
  1658.         return True
  1659.  
  1660.     
  1661.     def getAttribute(self, attribute):
  1662.         value = XMLSchemaComponent.getAttribute(self, attribute)
  1663.         if attribute != 'type' or value is not None:
  1664.             return value
  1665.         
  1666.         if self.content is not None:
  1667.             return None
  1668.         
  1669.         parent = self
  1670.         while None:
  1671.             nsdict = parent.attributes[XMLSchemaComponent.xmlns]
  1672.             for k, v in nsdict.items():
  1673.                 if v not in SCHEMA.XSD_LIST:
  1674.                     continue
  1675.                 
  1676.                 return TypeDescriptionComponent((v, 'anyType'))
  1677.             
  1678.             if isinstance(parent, WSDLToolsAdapter) or not hasattr(parent, '_parent'):
  1679.                 break
  1680.             
  1681.             parent = parent._parent()
  1682.             continue
  1683.             raise SchemaError, 'failed to locate the XSD namespace'
  1684.             return None
  1685.  
  1686.     
  1687.     def getElementDeclaration(self, attribute):
  1688.         raise Warning, 'invalid operation for <%s>' % self.tag
  1689.  
  1690.     
  1691.     def getTypeDefinition(self, attribute = None):
  1692.         if attribute:
  1693.             return XMLSchemaComponent.getTypeDefinition(self, attribute)
  1694.         
  1695.         gt = XMLSchemaComponent.getTypeDefinition(self, 'type')
  1696.         if gt:
  1697.             return gt
  1698.         
  1699.         return self.content
  1700.  
  1701.     
  1702.     def getConstraints(self):
  1703.         return self._constraints
  1704.  
  1705.     
  1706.     def setConstraints(self, constraints):
  1707.         self._constraints = tuple(constraints)
  1708.  
  1709.     constraints = property(getConstraints, setConstraints, None, 'tuple of key, keyref, unique constraints')
  1710.     
  1711.     def fromDom(self, node):
  1712.         self.setAttributes(node)
  1713.         contents = self.getContents(node)
  1714.         constraints = []
  1715.         for i in contents:
  1716.             component = SplitQName(i.getTagName())[1]
  1717.             if component in self.__class__.contents['xsd']:
  1718.                 if component == 'annotation' and not (self.annotation):
  1719.                     self.annotation = Annotation(self)
  1720.                     self.annotation.fromDom(i)
  1721.                 elif component == 'simpleType' and not (self.content):
  1722.                     self.content = AnonymousSimpleType(self)
  1723.                     self.content.fromDom(i)
  1724.                 elif component == 'complexType' and not (self.content):
  1725.                     self.content = LocalComplexType(self)
  1726.                     self.content.fromDom(i)
  1727.                 elif component == 'key':
  1728.                     constraints.append(Key(self))
  1729.                     constraints[-1].fromDom(i)
  1730.                 elif component == 'keyref':
  1731.                     constraints.append(KeyRef(self))
  1732.                     constraints[-1].fromDom(i)
  1733.                 elif component == 'unique':
  1734.                     constraints.append(Unique(self))
  1735.                     constraints[-1].fromDom(i)
  1736.                 else:
  1737.                     raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1738.             not (self.content)
  1739.             raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1740.         
  1741.         self.constraints = constraints
  1742.  
  1743.  
  1744.  
  1745. class LocalElementDeclaration(ElementDeclaration, LocalMarker):
  1746.     required = [
  1747.         'name']
  1748.     attributes = {
  1749.         'id': None,
  1750.         'name': None,
  1751.         'form': (lambda self: GetSchema(self).getElementFormDefault()),
  1752.         'type': None,
  1753.         'minOccurs': '1',
  1754.         'maxOccurs': '1',
  1755.         'default': None,
  1756.         'fixed': None,
  1757.         'nillable': 0,
  1758.         'abstract': 0,
  1759.         'block': (lambda self: GetSchema(self).getBlockDefault()) }
  1760.     contents = {
  1761.         'xsd': [
  1762.             'annotation',
  1763.             'simpleType',
  1764.             'complexType',
  1765.             'key',
  1766.             'keyref',
  1767.             'unique'] }
  1768.     
  1769.     def isQualified(self):
  1770.         form = self.getAttribute('form')
  1771.         if form == 'qualified':
  1772.             return True
  1773.         
  1774.         if form == 'unqualified':
  1775.             return False
  1776.         
  1777.         raise SchemaError, 'Bad form (%s) for element: %s' % (form, self.getItemTrace())
  1778.  
  1779.  
  1780.  
  1781. class ElementReference(XMLSchemaComponent, ElementMarker, ReferenceMarker):
  1782.     required = [
  1783.         'ref']
  1784.     attributes = {
  1785.         'id': None,
  1786.         'ref': None,
  1787.         'minOccurs': '1',
  1788.         'maxOccurs': '1' }
  1789.     contents = {
  1790.         'xsd': [
  1791.             'annotation'] }
  1792.     tag = 'element'
  1793.     
  1794.     def __init__(self, parent):
  1795.         XMLSchemaComponent.__init__(self, parent)
  1796.         self.annotation = None
  1797.  
  1798.     
  1799.     def getElementDeclaration(self, attribute = None):
  1800.         if attribute:
  1801.             return XMLSchemaComponent.getElementDeclaration(self, attribute)
  1802.         
  1803.         return XMLSchemaComponent.getElementDeclaration(self, 'ref')
  1804.  
  1805.     
  1806.     def fromDom(self, node):
  1807.         self.annotation = None
  1808.         self.setAttributes(node)
  1809.         for i in self.getContents(node):
  1810.             component = SplitQName(i.getTagName())[1]
  1811.             if component in self.__class__.contents['xsd']:
  1812.                 if component == 'annotation' and not (self.annotation):
  1813.                     self.annotation = Annotation(self)
  1814.                     self.annotation.fromDom(i)
  1815.                 else:
  1816.                     raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1817.             not (self.annotation)
  1818.         
  1819.  
  1820.  
  1821.  
  1822. class ElementWildCard(LocalElementDeclaration, WildCardMarker):
  1823.     required = []
  1824.     attributes = {
  1825.         'id': None,
  1826.         'minOccurs': '1',
  1827.         'maxOccurs': '1',
  1828.         'namespace': '##any',
  1829.         'processContents': 'strict' }
  1830.     contents = {
  1831.         'xsd': [
  1832.             'annotation'] }
  1833.     tag = 'any'
  1834.     
  1835.     def __init__(self, parent):
  1836.         XMLSchemaComponent.__init__(self, parent)
  1837.         self.annotation = None
  1838.  
  1839.     
  1840.     def isQualified(self):
  1841.         return GetSchema(self).isElementFormDefaultQualified()
  1842.  
  1843.     
  1844.     def getAttribute(self, attribute):
  1845.         return XMLSchemaComponent.getAttribute(self, attribute)
  1846.  
  1847.     
  1848.     def getTypeDefinition(self, attribute):
  1849.         raise Warning, 'invalid operation for <%s>' % self.tag
  1850.  
  1851.     
  1852.     def fromDom(self, node):
  1853.         self.annotation = None
  1854.         self.setAttributes(node)
  1855.         for i in self.getContents(node):
  1856.             component = SplitQName(i.getTagName())[1]
  1857.             if component in self.__class__.contents['xsd']:
  1858.                 if component == 'annotation' and not (self.annotation):
  1859.                     self.annotation = Annotation(self)
  1860.                     self.annotation.fromDom(i)
  1861.                 else:
  1862.                     raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1863.             not (self.annotation)
  1864.         
  1865.  
  1866.  
  1867.  
  1868. class Sequence(XMLSchemaComponent, SequenceMarker):
  1869.     attributes = {
  1870.         'id': None,
  1871.         'minOccurs': '1',
  1872.         'maxOccurs': '1' }
  1873.     contents = {
  1874.         'xsd': [
  1875.             'annotation',
  1876.             'element',
  1877.             'group',
  1878.             'choice',
  1879.             'sequence',
  1880.             'any'] }
  1881.     tag = 'sequence'
  1882.     
  1883.     def __init__(self, parent):
  1884.         XMLSchemaComponent.__init__(self, parent)
  1885.         self.annotation = None
  1886.         self.content = None
  1887.  
  1888.     
  1889.     def fromDom(self, node):
  1890.         self.setAttributes(node)
  1891.         contents = self.getContents(node)
  1892.         content = []
  1893.         for i in contents:
  1894.             component = SplitQName(i.getTagName())[1]
  1895.             if component in self.__class__.contents['xsd']:
  1896.                 if component == 'annotation' and not (self.annotation):
  1897.                     self.annotation = Annotation(self)
  1898.                     self.annotation.fromDom(i)
  1899.                     continue
  1900.                 elif component == 'element':
  1901.                     if i.hasattr('ref'):
  1902.                         content.append(ElementReference(self))
  1903.                     else:
  1904.                         content.append(LocalElementDeclaration(self))
  1905.                 elif component == 'group':
  1906.                     content.append(ModelGroupReference(self))
  1907.                 elif component == 'choice':
  1908.                     content.append(Choice(self))
  1909.                 elif component == 'sequence':
  1910.                     content.append(Sequence(self))
  1911.                 elif component == 'any':
  1912.                     content.append(ElementWildCard(self))
  1913.                 else:
  1914.                     raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1915.                 content[-1].fromDom(i)
  1916.                 continue
  1917.             raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1918.         
  1919.         self.content = tuple(content)
  1920.  
  1921.  
  1922.  
  1923. class All(XMLSchemaComponent, AllMarker):
  1924.     attributes = {
  1925.         'id': None,
  1926.         'minOccurs': '1',
  1927.         'maxOccurs': '1' }
  1928.     contents = {
  1929.         'xsd': [
  1930.             'annotation',
  1931.             'element'] }
  1932.     tag = 'all'
  1933.     
  1934.     def __init__(self, parent):
  1935.         XMLSchemaComponent.__init__(self, parent)
  1936.         self.annotation = None
  1937.         self.content = None
  1938.  
  1939.     
  1940.     def fromDom(self, node):
  1941.         self.setAttributes(node)
  1942.         contents = self.getContents(node)
  1943.         content = []
  1944.         for i in contents:
  1945.             component = SplitQName(i.getTagName())[1]
  1946.             if component in self.__class__.contents['xsd']:
  1947.                 if component == 'annotation' and not (self.annotation):
  1948.                     self.annotation = Annotation(self)
  1949.                     self.annotation.fromDom(i)
  1950.                     continue
  1951.                 elif component == 'element':
  1952.                     if i.hasattr('ref'):
  1953.                         content.append(ElementReference(self))
  1954.                     else:
  1955.                         content.append(LocalElementDeclaration(self))
  1956.                 else:
  1957.                     raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1958.                 content[-1].fromDom(i)
  1959.                 continue
  1960.             raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  1961.         
  1962.         self.content = tuple(content)
  1963.  
  1964.  
  1965.  
  1966. class Choice(XMLSchemaComponent, ChoiceMarker):
  1967.     attributes = {
  1968.         'id': None,
  1969.         'minOccurs': '1',
  1970.         'maxOccurs': '1' }
  1971.     contents = {
  1972.         'xsd': [
  1973.             'annotation',
  1974.             'element',
  1975.             'group',
  1976.             'choice',
  1977.             'sequence',
  1978.             'any'] }
  1979.     tag = 'choice'
  1980.     
  1981.     def __init__(self, parent):
  1982.         XMLSchemaComponent.__init__(self, parent)
  1983.         self.annotation = None
  1984.         self.content = None
  1985.  
  1986.     
  1987.     def fromDom(self, node):
  1988.         self.setAttributes(node)
  1989.         contents = self.getContents(node)
  1990.         content = []
  1991.         for i in contents:
  1992.             component = SplitQName(i.getTagName())[1]
  1993.             if component in self.__class__.contents['xsd']:
  1994.                 if component == 'annotation' and not (self.annotation):
  1995.                     self.annotation = Annotation(self)
  1996.                     self.annotation.fromDom(i)
  1997.                     continue
  1998.                 elif component == 'element':
  1999.                     if i.hasattr('ref'):
  2000.                         content.append(ElementReference(self))
  2001.                     else:
  2002.                         content.append(LocalElementDeclaration(self))
  2003.                 elif component == 'group':
  2004.                     content.append(ModelGroupReference(self))
  2005.                 elif component == 'choice':
  2006.                     content.append(Choice(self))
  2007.                 elif component == 'sequence':
  2008.                     content.append(Sequence(self))
  2009.                 elif component == 'any':
  2010.                     content.append(ElementWildCard(self))
  2011.                 else:
  2012.                     raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  2013.                 content[-1].fromDom(i)
  2014.                 continue
  2015.             raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  2016.         
  2017.         self.content = tuple(content)
  2018.  
  2019.  
  2020.  
  2021. class ModelGroupDefinition(XMLSchemaComponent, ModelGroupMarker, DefinitionMarker):
  2022.     required = [
  2023.         'name']
  2024.     attributes = {
  2025.         'id': None,
  2026.         'name': None }
  2027.     contents = {
  2028.         'xsd': [
  2029.             'annotation',
  2030.             'all',
  2031.             'choice',
  2032.             'sequence'] }
  2033.     tag = 'group'
  2034.     
  2035.     def __init__(self, parent):
  2036.         XMLSchemaComponent.__init__(self, parent)
  2037.         self.annotation = None
  2038.         self.content = None
  2039.  
  2040.     
  2041.     def fromDom(self, node):
  2042.         self.setAttributes(node)
  2043.         contents = self.getContents(node)
  2044.         for i in contents:
  2045.             component = SplitQName(i.getTagName())[1]
  2046.             if component in self.__class__.contents['xsd']:
  2047.                 if component == 'annotation' and not (self.annotation):
  2048.                     self.annotation = Annotation(self)
  2049.                     self.annotation.fromDom(i)
  2050.                     continue
  2051.                 elif component == 'all' and not (self.content):
  2052.                     self.content = All(self)
  2053.                 elif component == 'choice' and not (self.content):
  2054.                     self.content = Choice(self)
  2055.                 elif component == 'sequence' and not (self.content):
  2056.                     self.content = Sequence(self)
  2057.                 else:
  2058.                     raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  2059.                 self.content.fromDom(i)
  2060.                 continue
  2061.             raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  2062.         
  2063.  
  2064.  
  2065.  
  2066. class ModelGroupReference(XMLSchemaComponent, ModelGroupMarker, ReferenceMarker):
  2067.     required = [
  2068.         'ref']
  2069.     attributes = {
  2070.         'id': None,
  2071.         'ref': None,
  2072.         'minOccurs': '1',
  2073.         'maxOccurs': '1' }
  2074.     contents = {
  2075.         'xsd': [
  2076.             'annotation'] }
  2077.     tag = 'group'
  2078.     
  2079.     def __init__(self, parent):
  2080.         XMLSchemaComponent.__init__(self, parent)
  2081.         self.annotation = None
  2082.  
  2083.     
  2084.     def getModelGroupReference(self):
  2085.         return self.getModelGroup('ref')
  2086.  
  2087.     
  2088.     def fromDom(self, node):
  2089.         self.setAttributes(node)
  2090.         contents = self.getContents(node)
  2091.         for i in contents:
  2092.             component = SplitQName(i.getTagName())[1]
  2093.             if component in self.__class__.contents['xsd']:
  2094.                 if component == 'annotation' and not (self.annotation):
  2095.                     self.annotation = Annotation(self)
  2096.                     self.annotation.fromDom(i)
  2097.                 else:
  2098.                     raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  2099.             not (self.annotation)
  2100.             raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  2101.         
  2102.  
  2103.  
  2104.  
  2105. class ComplexType(XMLSchemaComponent, DefinitionMarker, ComplexMarker):
  2106.     required = [
  2107.         'name']
  2108.     attributes = {
  2109.         'id': None,
  2110.         'name': None,
  2111.         'mixed': 0,
  2112.         'abstract': 0,
  2113.         'block': (lambda self: self._parent().getBlockDefault()),
  2114.         'final': (lambda self: self._parent().getFinalDefault()) }
  2115.     contents = {
  2116.         'xsd': [
  2117.             'annotation',
  2118.             'simpleContent',
  2119.             'complexContent',
  2120.             'group',
  2121.             'all',
  2122.             'choice',
  2123.             'sequence',
  2124.             'attribute',
  2125.             'attributeGroup',
  2126.             'anyAttribute',
  2127.             'any'] }
  2128.     tag = 'complexType'
  2129.     
  2130.     def __init__(self, parent):
  2131.         XMLSchemaComponent.__init__(self, parent)
  2132.         self.annotation = None
  2133.         self.content = None
  2134.         self.attr_content = None
  2135.  
  2136.     
  2137.     def isMixed(self):
  2138.         m = self.getAttribute('mixed')
  2139.         if m == 0 or m == False:
  2140.             return False
  2141.         
  2142.         if isinstance(m, basestring) is True:
  2143.             if m in ('false', '0'):
  2144.                 return False
  2145.             
  2146.             if m in ('true', '1'):
  2147.                 return True
  2148.             
  2149.         
  2150.         raise SchemaError, 'invalid value for attribute mixed(%s): %s' % (m, self.getItemTrace())
  2151.  
  2152.     
  2153.     def getAttributeContent(self):
  2154.         return self.attr_content
  2155.  
  2156.     
  2157.     def getElementDeclaration(self, attribute):
  2158.         raise Warning, 'invalid operation for <%s>' % self.tag
  2159.  
  2160.     
  2161.     def getTypeDefinition(self, attribute):
  2162.         raise Warning, 'invalid operation for <%s>' % self.tag
  2163.  
  2164.     
  2165.     def fromDom(self, node):
  2166.         self.setAttributes(node)
  2167.         contents = self.getContents(node)
  2168.         indx = 0
  2169.         num = len(contents)
  2170.         if not num:
  2171.             return None
  2172.         
  2173.         component = SplitQName(contents[indx].getTagName())[1]
  2174.         if component == 'annotation':
  2175.             self.annotation = Annotation(self)
  2176.             self.annotation.fromDom(contents[indx])
  2177.             indx += 1
  2178.             component = SplitQName(contents[indx].getTagName())[1]
  2179.         
  2180.         self.content = None
  2181.         if component == 'simpleContent':
  2182.             self.content = self.__class__.SimpleContent(self)
  2183.             self.content.fromDom(contents[indx])
  2184.         elif component == 'complexContent':
  2185.             self.content = self.__class__.ComplexContent(self)
  2186.             self.content.fromDom(contents[indx])
  2187.         elif component == 'all':
  2188.             self.content = All(self)
  2189.         elif component == 'choice':
  2190.             self.content = Choice(self)
  2191.         elif component == 'sequence':
  2192.             self.content = Sequence(self)
  2193.         elif component == 'group':
  2194.             self.content = ModelGroupReference(self)
  2195.         
  2196.         if self.content:
  2197.             self.content.fromDom(contents[indx])
  2198.             indx += 1
  2199.         
  2200.         self.attr_content = []
  2201.         while indx < num:
  2202.             component = SplitQName(contents[indx].getTagName())[1]
  2203.             if component == 'attribute':
  2204.                 if contents[indx].hasattr('ref'):
  2205.                     self.attr_content.append(AttributeReference(self))
  2206.                 else:
  2207.                     self.attr_content.append(LocalAttributeDeclaration(self))
  2208.             elif component == 'attributeGroup':
  2209.                 self.attr_content.append(AttributeGroupReference(self))
  2210.             elif component == 'anyAttribute':
  2211.                 self.attr_content.append(AttributeWildCard(self))
  2212.             else:
  2213.                 raise SchemaError, 'Unknown component (%s): %s' % (contents[indx].getTagName(), self.getItemTrace())
  2214.             self.attr_content[-1].fromDom(contents[indx])
  2215.             indx += 1
  2216.  
  2217.     
  2218.     class _DerivedType(XMLSchemaComponent):
  2219.         
  2220.         def __init__(self, parent):
  2221.             XMLSchemaComponent.__init__(self, parent)
  2222.             self.annotation = None
  2223.             self.derivation = None
  2224.             self.content = None
  2225.  
  2226.         
  2227.         def fromDom(self, node):
  2228.             self.setAttributes(node)
  2229.             contents = self.getContents(node)
  2230.             for i in contents:
  2231.                 component = SplitQName(i.getTagName())[1]
  2232.                 if component in self.__class__.contents['xsd']:
  2233.                     if component == 'annotation' and not (self.annotation):
  2234.                         self.annotation = Annotation(self)
  2235.                         self.annotation.fromDom(i)
  2236.                         continue
  2237.                     elif component == 'restriction' and not (self.derivation):
  2238.                         self.derivation = self.__class__.Restriction(self)
  2239.                     elif component == 'extension' and not (self.derivation):
  2240.                         self.derivation = self.__class__.Extension(self)
  2241.                     else:
  2242.                         raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  2243.                 else:
  2244.                     raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  2245.                 self.derivation.fromDom(i)
  2246.             
  2247.             self.content = self.derivation
  2248.  
  2249.  
  2250.     
  2251.     class ComplexContent(_DerivedType, ComplexMarker):
  2252.         attributes = {
  2253.             'id': None,
  2254.             'mixed': 0 }
  2255.         contents = {
  2256.             'xsd': [
  2257.                 'annotation',
  2258.                 'restriction',
  2259.                 'extension'] }
  2260.         tag = 'complexContent'
  2261.         
  2262.         def isMixed(self):
  2263.             m = self.getAttribute('mixed')
  2264.             if m == 0 or m == False:
  2265.                 return False
  2266.             
  2267.             if isinstance(m, basestring) is True:
  2268.                 if m in ('false', '0'):
  2269.                     return False
  2270.                 
  2271.                 if m in ('true', '1'):
  2272.                     return True
  2273.                 
  2274.             
  2275.             raise SchemaError, 'invalid value for attribute mixed(%s): %s' % (m, self.getItemTrace())
  2276.  
  2277.         
  2278.         class _DerivationBase(XMLSchemaComponent):
  2279.             required = [
  2280.                 'base']
  2281.             attributes = {
  2282.                 'id': None,
  2283.                 'base': None }
  2284.             contents = {
  2285.                 'xsd': [
  2286.                     'annotation',
  2287.                     'group',
  2288.                     'all',
  2289.                     'choice',
  2290.                     'sequence',
  2291.                     'attribute',
  2292.                     'attributeGroup',
  2293.                     'anyAttribute'] }
  2294.             
  2295.             def __init__(self, parent):
  2296.                 XMLSchemaComponent.__init__(self, parent)
  2297.                 self.annotation = None
  2298.                 self.content = None
  2299.                 self.attr_content = None
  2300.  
  2301.             
  2302.             def getAttributeContent(self):
  2303.                 return self.attr_content
  2304.  
  2305.             
  2306.             def fromDom(self, node):
  2307.                 self.setAttributes(node)
  2308.                 contents = self.getContents(node)
  2309.                 indx = 0
  2310.                 num = len(contents)
  2311.                 if not num:
  2312.                     return None
  2313.                 
  2314.                 component = SplitQName(contents[indx].getTagName())[1]
  2315.                 if component == 'annotation':
  2316.                     self.annotation = Annotation(self)
  2317.                     self.annotation.fromDom(contents[indx])
  2318.                     indx += 1
  2319.                     component = SplitQName(contents[indx].getTagName())[1]
  2320.                 
  2321.                 if component == 'all':
  2322.                     self.content = All(self)
  2323.                     self.content.fromDom(contents[indx])
  2324.                     indx += 1
  2325.                 elif component == 'choice':
  2326.                     self.content = Choice(self)
  2327.                     self.content.fromDom(contents[indx])
  2328.                     indx += 1
  2329.                 elif component == 'sequence':
  2330.                     self.content = Sequence(self)
  2331.                     self.content.fromDom(contents[indx])
  2332.                     indx += 1
  2333.                 elif component == 'group':
  2334.                     self.content = ModelGroupReference(self)
  2335.                     self.content.fromDom(contents[indx])
  2336.                     indx += 1
  2337.                 else:
  2338.                     self.content = None
  2339.                 self.attr_content = []
  2340.                 while indx < num:
  2341.                     component = SplitQName(contents[indx].getTagName())[1]
  2342.                     if component == 'attribute':
  2343.                         if contents[indx].hasattr('ref'):
  2344.                             self.attr_content.append(AttributeReference(self))
  2345.                         else:
  2346.                             self.attr_content.append(LocalAttributeDeclaration(self))
  2347.                     elif component == 'attributeGroup':
  2348.                         if contents[indx].hasattr('ref'):
  2349.                             self.attr_content.append(AttributeGroupReference(self))
  2350.                         else:
  2351.                             self.attr_content.append(AttributeGroupDefinition(self))
  2352.                     elif component == 'anyAttribute':
  2353.                         self.attr_content.append(AttributeWildCard(self))
  2354.                     else:
  2355.                         raise SchemaError, 'Unknown component (%s)' % contents[indx].getTagName()
  2356.                     self.attr_content[-1].fromDom(contents[indx])
  2357.                     indx += 1
  2358.  
  2359.  
  2360.         
  2361.         class Extension(_DerivationBase, ExtensionMarker):
  2362.             tag = 'extension'
  2363.  
  2364.         
  2365.         class Restriction(_DerivationBase, RestrictionMarker):
  2366.             tag = 'restriction'
  2367.  
  2368.  
  2369.     
  2370.     class SimpleContent(_DerivedType, SimpleMarker):
  2371.         attributes = {
  2372.             'id': None }
  2373.         contents = {
  2374.             'xsd': [
  2375.                 'annotation',
  2376.                 'restriction',
  2377.                 'extension'] }
  2378.         tag = 'simpleContent'
  2379.         
  2380.         class Extension(XMLSchemaComponent, ExtensionMarker):
  2381.             required = [
  2382.                 'base']
  2383.             attributes = {
  2384.                 'id': None,
  2385.                 'base': None }
  2386.             contents = {
  2387.                 'xsd': [
  2388.                     'annotation',
  2389.                     'attribute',
  2390.                     'attributeGroup',
  2391.                     'anyAttribute'] }
  2392.             tag = 'extension'
  2393.             
  2394.             def __init__(self, parent):
  2395.                 XMLSchemaComponent.__init__(self, parent)
  2396.                 self.annotation = None
  2397.                 self.attr_content = None
  2398.  
  2399.             
  2400.             def getAttributeContent(self):
  2401.                 return self.attr_content
  2402.  
  2403.             
  2404.             def fromDom(self, node):
  2405.                 self.setAttributes(node)
  2406.                 contents = self.getContents(node)
  2407.                 indx = 0
  2408.                 num = len(contents)
  2409.                 if num:
  2410.                     component = SplitQName(contents[indx].getTagName())[1]
  2411.                     if component == 'annotation':
  2412.                         self.annotation = Annotation(self)
  2413.                         self.annotation.fromDom(contents[indx])
  2414.                         indx += 1
  2415.                         component = SplitQName(contents[indx].getTagName())[1]
  2416.                     
  2417.                 
  2418.                 content = []
  2419.                 while indx < num:
  2420.                     component = SplitQName(contents[indx].getTagName())[1]
  2421.                     if component == 'attribute':
  2422.                         if contents[indx].hasattr('ref'):
  2423.                             content.append(AttributeReference(self))
  2424.                         else:
  2425.                             content.append(LocalAttributeDeclaration(self))
  2426.                     elif component == 'attributeGroup':
  2427.                         content.append(AttributeGroupReference(self))
  2428.                     elif component == 'anyAttribute':
  2429.                         content.append(AttributeWildCard(self))
  2430.                     else:
  2431.                         raise SchemaError, 'Unknown component (%s)' % contents[indx].getTagName()
  2432.                     content[-1].fromDom(contents[indx])
  2433.                     indx += 1
  2434.                 self.attr_content = tuple(content)
  2435.  
  2436.  
  2437.         
  2438.         class Restriction(XMLSchemaComponent, RestrictionMarker):
  2439.             required = [
  2440.                 'base']
  2441.             attributes = {
  2442.                 'id': None,
  2443.                 'base': None }
  2444.             contents = {
  2445.                 'xsd': [
  2446.                     'annotation',
  2447.                     'simpleType',
  2448.                     'attribute',
  2449.                     'attributeGroup',
  2450.                     'anyAttribute'] + RestrictionMarker.facets }
  2451.             tag = 'restriction'
  2452.             
  2453.             def __init__(self, parent):
  2454.                 XMLSchemaComponent.__init__(self, parent)
  2455.                 self.annotation = None
  2456.                 self.content = None
  2457.                 self.attr_content = None
  2458.  
  2459.             
  2460.             def getAttributeContent(self):
  2461.                 return self.attr_content
  2462.  
  2463.             
  2464.             def fromDom(self, node):
  2465.                 self.content = []
  2466.                 self.setAttributes(node)
  2467.                 contents = self.getContents(node)
  2468.                 indx = 0
  2469.                 num = len(contents)
  2470.                 component = SplitQName(contents[indx].getTagName())[1]
  2471.                 if component == 'annotation':
  2472.                     self.annotation = Annotation(self)
  2473.                     self.annotation.fromDom(contents[indx])
  2474.                     indx += 1
  2475.                     component = SplitQName(contents[indx].getTagName())[1]
  2476.                 
  2477.                 content = []
  2478.                 while indx < num:
  2479.                     component = SplitQName(contents[indx].getTagName())[1]
  2480.                     if component == 'attribute':
  2481.                         if contents[indx].hasattr('ref'):
  2482.                             content.append(AttributeReference(self))
  2483.                         else:
  2484.                             content.append(LocalAttributeDeclaration(self))
  2485.                     elif component == 'attributeGroup':
  2486.                         content.append(AttributeGroupReference(self))
  2487.                     elif component == 'anyAttribute':
  2488.                         content.append(AttributeWildCard(self))
  2489.                     elif component == 'simpleType':
  2490.                         self.content.append(AnonymousSimpleType(self))
  2491.                         self.content[-1].fromDom(contents[indx])
  2492.                     else:
  2493.                         raise SchemaError, 'Unknown component (%s)' % contents[indx].getTagName()
  2494.                     content[-1].fromDom(contents[indx])
  2495.                     indx += 1
  2496.                 self.attr_content = tuple(content)
  2497.  
  2498.  
  2499.  
  2500.  
  2501.  
  2502. class LocalComplexType(ComplexType, LocalMarker):
  2503.     required = []
  2504.     attributes = {
  2505.         'id': None,
  2506.         'mixed': 0 }
  2507.     tag = 'complexType'
  2508.  
  2509.  
  2510. class SimpleType(XMLSchemaComponent, DefinitionMarker, SimpleMarker):
  2511.     required = [
  2512.         'name']
  2513.     attributes = {
  2514.         'id': None,
  2515.         'name': None,
  2516.         'final': (lambda self: self._parent().getFinalDefault()) }
  2517.     contents = {
  2518.         'xsd': [
  2519.             'annotation',
  2520.             'restriction',
  2521.             'list',
  2522.             'union'] }
  2523.     tag = 'simpleType'
  2524.     
  2525.     def __init__(self, parent):
  2526.         XMLSchemaComponent.__init__(self, parent)
  2527.         self.annotation = None
  2528.         self.content = None
  2529.  
  2530.     
  2531.     def getElementDeclaration(self, attribute):
  2532.         raise Warning, 'invalid operation for <%s>' % self.tag
  2533.  
  2534.     
  2535.     def getTypeDefinition(self, attribute):
  2536.         raise Warning, 'invalid operation for <%s>' % self.tag
  2537.  
  2538.     
  2539.     def fromDom(self, node):
  2540.         self.setAttributes(node)
  2541.         contents = self.getContents(node)
  2542.         for child in contents:
  2543.             component = SplitQName(child.getTagName())[1]
  2544.             if component == 'annotation':
  2545.                 self.annotation = Annotation(self)
  2546.                 self.annotation.fromDom(child)
  2547.                 continue
  2548.             
  2549.             break
  2550.         else:
  2551.             return None
  2552.         if component == 'restriction':
  2553.             self.content = self.__class__.Restriction(self)
  2554.         elif component == 'list':
  2555.             self.content = self.__class__.List(self)
  2556.         elif component == 'union':
  2557.             self.content = self.__class__.Union(self)
  2558.         else:
  2559.             raise SchemaError, 'Unknown component (%s)' % component
  2560.         self.content.fromDom(child)
  2561.  
  2562.     
  2563.     class Restriction(XMLSchemaComponent, RestrictionMarker):
  2564.         attributes = {
  2565.             'id': None,
  2566.             'base': None }
  2567.         contents = {
  2568.             'xsd': [
  2569.                 'annotation',
  2570.                 'simpleType'] + RestrictionMarker.facets }
  2571.         tag = 'restriction'
  2572.         
  2573.         def __init__(self, parent):
  2574.             XMLSchemaComponent.__init__(self, parent)
  2575.             self.annotation = None
  2576.             self.content = None
  2577.             self.facets = None
  2578.  
  2579.         
  2580.         def getAttributeBase(self):
  2581.             return XMLSchemaComponent.getAttribute(self, 'base')
  2582.  
  2583.         
  2584.         def getTypeDefinition(self, attribute = 'base'):
  2585.             return XMLSchemaComponent.getTypeDefinition(self, attribute)
  2586.  
  2587.         
  2588.         def getSimpleTypeContent(self):
  2589.             for el in self.content:
  2590.                 if el.isSimple():
  2591.                     return el
  2592.                     continue
  2593.             
  2594.  
  2595.         
  2596.         def fromDom(self, node):
  2597.             self.facets = []
  2598.             self.setAttributes(node)
  2599.             contents = self.getContents(node)
  2600.             content = []
  2601.             for indx in range(len(contents)):
  2602.                 component = SplitQName(contents[indx].getTagName())[1]
  2603.                 if component == 'annotation' and not indx:
  2604.                     self.annotation = Annotation(self)
  2605.                     self.annotation.fromDom(contents[indx])
  2606.                     continue
  2607.                     continue
  2608.                 if component == 'simpleType':
  2609.                     if not indx or indx == 1:
  2610.                         content.append(AnonymousSimpleType(self))
  2611.                         content[-1].fromDom(contents[indx])
  2612.                         continue
  2613.                 if component in RestrictionMarker.facets:
  2614.                     self.facets.append(contents[indx])
  2615.                     continue
  2616.                 raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  2617.             
  2618.             self.content = tuple(content)
  2619.  
  2620.  
  2621.     
  2622.     class Union(XMLSchemaComponent, UnionMarker):
  2623.         attributes = {
  2624.             'id': None,
  2625.             'memberTypes': None }
  2626.         contents = {
  2627.             'xsd': [
  2628.                 'annotation',
  2629.                 'simpleType'] }
  2630.         tag = 'union'
  2631.         
  2632.         def __init__(self, parent):
  2633.             XMLSchemaComponent.__init__(self, parent)
  2634.             self.annotation = None
  2635.             self.content = None
  2636.  
  2637.         
  2638.         def fromDom(self, node):
  2639.             self.setAttributes(node)
  2640.             contents = self.getContents(node)
  2641.             content = []
  2642.             for indx in range(len(contents)):
  2643.                 component = SplitQName(contents[indx].getTagName())[1]
  2644.                 if component == 'annotation' and not indx:
  2645.                     self.annotation = Annotation(self)
  2646.                     self.annotation.fromDom(contents[indx])
  2647.                     continue
  2648.                 if component == 'simpleType':
  2649.                     content.append(AnonymousSimpleType(self))
  2650.                     content[-1].fromDom(contents[indx])
  2651.                     continue
  2652.                 raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  2653.             
  2654.             self.content = tuple(content)
  2655.  
  2656.  
  2657.     
  2658.     class List(XMLSchemaComponent, ListMarker):
  2659.         attributes = {
  2660.             'id': None,
  2661.             'itemType': None }
  2662.         contents = {
  2663.             'xsd': [
  2664.                 'annotation',
  2665.                 'simpleType'] }
  2666.         tag = 'list'
  2667.         
  2668.         def __init__(self, parent):
  2669.             XMLSchemaComponent.__init__(self, parent)
  2670.             self.annotation = None
  2671.             self.content = None
  2672.  
  2673.         
  2674.         def getItemType(self):
  2675.             return self.attributes.get('itemType')
  2676.  
  2677.         
  2678.         def getTypeDefinition(self, attribute = 'itemType'):
  2679.             tp = XMLSchemaComponent.getTypeDefinition(self, attribute)
  2680.             if not tp:
  2681.                 pass
  2682.             return self.content
  2683.  
  2684.         
  2685.         def fromDom(self, node):
  2686.             self.annotation = None
  2687.             self.content = None
  2688.             self.setAttributes(node)
  2689.             contents = self.getContents(node)
  2690.             for indx in range(len(contents)):
  2691.                 component = SplitQName(contents[indx].getTagName())[1]
  2692.                 if component == 'annotation' and not indx:
  2693.                     self.annotation = Annotation(self)
  2694.                     self.annotation.fromDom(contents[indx])
  2695.                     continue
  2696.                 if component == 'simpleType':
  2697.                     self.content = AnonymousSimpleType(self)
  2698.                     self.content.fromDom(contents[indx])
  2699.                     break
  2700.                     continue
  2701.                 raise SchemaError, 'Unknown component (%s)' % i.getTagName()
  2702.             
  2703.  
  2704.  
  2705.  
  2706.  
  2707. class AnonymousSimpleType(SimpleType, SimpleMarker, LocalMarker):
  2708.     required = []
  2709.     attributes = {
  2710.         'id': None }
  2711.     tag = 'simpleType'
  2712.  
  2713.  
  2714. class Redefine:
  2715.     tag = 'redefine'
  2716.  
  2717. if sys.version_info[:2] >= (2, 2):
  2718.     tupleClass = tuple
  2719. else:
  2720.     import UserTuple
  2721.     tupleClass = UserTuple.UserTuple
  2722.  
  2723. class TypeDescriptionComponent(tupleClass):
  2724.     
  2725.     def __init__(self, args):
  2726.         if len(args) != 2:
  2727.             raise TypeError, 'expecting tuple (namespace, name), got %s' % args
  2728.         elif args[1].find(':') >= 0:
  2729.             args = (args[0], SplitQName(args[1])[1])
  2730.         
  2731.         tuple.__init__(self, args)
  2732.  
  2733.     
  2734.     def getTargetNamespace(self):
  2735.         return self[0]
  2736.  
  2737.     
  2738.     def getName(self):
  2739.         return self[1]
  2740.  
  2741.  
  2742.